'use strict';
const dayjs = require("dayjs");

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;
  
  const LibraryAreas = app.model.define(
    'LibraryAreas',
    {
      id: { type: INTEGER, primaryKey: true, autoIncrement: true },
      library_id: { type: INTEGER, allowNull: false },
      floor_id: { type: INTEGER, allowNull: false },
      name: { type: STRING(50), allowNull: false },
      description: { type: STRING(255) },
      status: { type: INTEGER, defaultValue: 1 },
      create_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      },
      update_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      }
    },
    {
      tableName: 'library_areas',
      timestamps: true,
      createdAt: 'create_time',
      updatedAt: 'update_time'
    }
  );

  LibraryAreas.associate = function() {
    app.model.LibraryAreas.belongsTo(app.model.Libraries, { foreignKey: 'library_id', targetKey: 'id', as: 'library' });
    app.model.LibraryAreas.belongsTo(app.model.LibraryFloors, { foreignKey: 'floor_id', targetKey: 'id', as: 'floor' });
    app.model.LibraryAreas.hasMany(app.model.Seats, { foreignKey: 'area_id', as: 'seats' });
  };

  return LibraryAreas;
};
